package com.veer.shop.service.impl.admin;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.veer.shop.config.ResponseCodeEnum;
import com.veer.shop.mapper.ProductBrandMapper;
import com.veer.shop.param.BrandParam;
import com.veer.shop.pojo.ProductBrand;
import com.veer.shop.pojo.ResponseData;
import com.veer.shop.pojo.ValidResult;
import com.veer.shop.service.admin.BrandService;
import com.veer.shop.utils.DateTimeUtil;
import com.veer.shop.utils.InitUtil;
import com.veer.shop.utils.ResponseUtil;

/**
 * 商品品牌业务实现类
 * 
 * @author Wangbin
 * 2020年9月22日-上午9:45:59
 */
@Service
public class BrandServiceImpl implements BrandService {
	
	@Autowired
	ProductBrandMapper productBrandMapper;

	/**
	 * 新增|更新品牌
	 */
	@Override
	public ResponseData<?> update(ProductBrand brand) {
		// TODO Auto-generated method stub
		// 校验参数
		ValidResult verifyResult = verifyBrand(brand);
		int count = 0;
		if(verifyResult.getCode() == 0) {
			if(brand.getBrandId() > 0) {
				// 更新品牌
				count = productBrandMapper.update(brand);
			} else {
				// 新增品牌
				brand.setCreatedAt(DateTimeUtil.getDateTime());
				brand.setUpdatedAt(brand.getCreatedAt());
				
				count = productBrandMapper.insert(brand);
			}
			
			if(count > 0) {
				return ResponseUtil.success();
			} else {
				return ResponseUtil.error();
			}
		} else {
			return ResponseUtil.error(verifyResult.getMessage());
		}
	}

	/**
	 * 品牌列表
	 */
	@Override
	public ResponseData<?> getAll(BrandParam brandListParam, int pageNo, int pageSize) {
		Map<String, Object> listResultMap = InitUtil.initListResultMap();
		Page page = PageMethod.startPage(pageNo, pageSize);
		List<Map<String, Object>> brands = productBrandMapper.selectAll(brandListParam);
		listResultMap.put("count", page.getTotal());
		listResultMap.put("list", brands);
		return ResponseUtil.success(listResultMap);
	}

	/**
	 * 删除品牌
	 */
	@Override
	public ResponseData<?> delete(String brandId) {
		
		// 处理品牌ID
		String[] brandIdList = brandId.split(",");
		for(String id : brandIdList) {
			int bid = Integer.parseInt(id);
			
			// 查询品牌
			ProductBrand findBrand = productBrandMapper.selectById(bid);
			if(findBrand == null)continue;
			
			productBrandMapper.delete(bid);
		}
		
		return ResponseUtil.success();
	}

	/**
	 * 品牌详情
	 */
	@Override
	public ResponseData<?> get(int brand_id) {
		// 查询品牌
		ProductBrand findBrand = productBrandMapper.selectById(brand_id);
		
		if(findBrand == null)return ResponseUtil.error(ResponseCodeEnum.VIEW_PRODUCT_BRAND_ERROR.getMessage());
		
		return ResponseUtil.success(findBrand);
	}

	/**
	 * 校验品牌参数合法性
	 * 
	 * @param brand
	 * @return
	 */
	private ValidResult verifyBrand(ProductBrand brand) {
		
		ValidResult result = InitUtil.initFaildMap();
		
		if(brand.getBrandName().equals("")) {
			result.setMessage("品牌名称不可为空！");
			return result;
		}
		
		if(brand.getBrandBn().equals("")) {
			result.setMessage("品牌编码不可为空！");
			return result;
		}
		
		if(brand.getBrandId() > 0) {
			// 查询品牌
			ProductBrand findBrand = productBrandMapper.selectById(brand.getBrandId());
			
			if(findBrand == null) {
				result.setMessage("品牌不存在！");
				return result;
			}
		}
		
		int count = productBrandMapper.search(brand.getBrandName(), brand.getBrandBn(), brand.getBrandId());
		
		if(count > 0) {
			result.setMessage("名称或编码已存在！");
			return result;
		}
		
		return InitUtil.initSuccMap();
	}
}
